<template>
  <div>
    <TopFilter :list="topFilterList" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="tableChangeFun"
      :onRowSelectChange="selectChange"
    >
    </FilterTable>

    <div class="dialogBottmView">
      <el-button size="small" type="primary" class="btnStyle" @click="submitBtn">确定 </el-button>
      <el-button size="small" type="primary" class="btnStyle" @click="closeDialog">关闭 </el-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { getVinList } from '@/api/repair/batchEventManagement/activityDistribution';
import { isEmpty } from '@/utils/utils';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { getAllModel } from '@/api/repair/common/index.js';

export default {
  name: 'addVin',
  components: {},
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: getVinList,
      fetchParams: {},
      columns: this.createTableColumns(),
      selectArr: []
    };
  },
  components: {},

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getAllDelears(this.columns, ['salesCode', 'ownerCode'], 'table');
    this.getAllModel();
  },
  methods: {
    async getAllModel() {
      let res = await getAllModel();
      if (res.resultCode == 200) {
        this.topFilterList.find(x => x.fieldName === 'modelId').itemList = res.data.map(x => {
          return { text: x.modelName, value: x.modelId + '' };
        });
      }
    },

    tableChangeFun(data) {
      // this.BaseTable.DO_REFRESH(false)
    },
    selectChange(val) {
      this.selectArr = val;
      console.log(val);
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 150,
          sorter: true,
          filter: false,
          filterType: 'input'
        },
        {
          title: '次数',
          dataIndex: 'counts',
          width: 150,
          sorter: true,
          defaultEditable: true,
          editable: true,
          editType: 'number',
          editValidator: (val, row, callback) => {
            if (row.counts <= 0) {
              this.$notify.error({ title: '提示信息', message: '更新数量不得小于1' || '操作失败!' });
              row.counts = 1;
            }
          },
          editRequired: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车辆状态',
          dataIndex: 'vehicleStatus',
          width: 150,
          sorter: true,
          filter: false,
          filterType: 'checkbox',
          filterItems: this.createDictList(6040),
          render: props => {
            return <span>{this.createDictText(props.row.vehicleStatus, 6040)}</span>;
          }
        },
        {
          title: '销售经销商',
          dataIndex: 'salesCode',
          width: 150,
          sorter: true,
          filter: false,
          dictItems: [],
          filterItems: [],
          filterType: 'checkbox'
        },
        {
          title: '责任经销商',
          dataIndex: 'ownerCode',
          width: 150,
          sorter: true,
          filter: false,
          dictItems: [],
          filterItems: [],
          filterType: 'checkbox'
        },

        {
          title: '生产日期',
          dataIndex: 'productDate',
          sorter: true,
          filter: false,
          filterType: 'date-range'
        },
        {
          title: '销售日期',
          dataIndex: 'salesDate',
          sorter: true,
          filter: false,
          filterType: 'date-range'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: false,
          filterType: 'input'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: 'VIN...'
        },
        {
          type: 'RANGE_DATE',
          label: '生产日期',
          dateType: 'daterange',
          // initialValue: [
          //   moment(new Date())
          //     .subtract(1, 'months')
          //     .format('YYYY-MM-DD'),
          //   moment(new Date()).format('YYYY-MM-DD')
          // ],
          style: { minWidth: '200px' },
          fieldName: 'bProductionDate|eProductionDate'
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelId',
          placeholder: '车型...',
          itemList: []
        }
      ];
    },

    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    changeHandle(val) {
      // val.bDate=moment(val['bDate']).format('YYYY-MM-DD')
      // val.eDate=moment(val['eDate']).format('YYYY-MM-DD')
      this.fetchParams = Object.assign({}, val);

      console.log(this.fetchParams);
    },

    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    closeDialog() {
      this.$emit('closeVin');
    },
    submitBtn() {
      if (this.selectArr.length == 0) {
        this.$notify.warning({ title: '提示信息', message: '请您选择vin' });
        return;
      }

      this.selectArr.forEach((item, index) => {
        item['vinStatus'] = isEmpty(item['vehicleStatus']) ? '' : item['vehicleStatus'];
        item['resDealer'] = isEmpty(item['ownerCode']) ? '' : item['ownerCode'];
        item['salesDealer'] = isEmpty(item['salesCode']) ? '' : item['salesCode'];
        item['productionDate'] = isEmpty(item['productDate']) ? '' : item['productDate'];
      });

      this.$emit('selectArr', this.selectArr);
    }
  }
};
</script>

<style lang="less" scoped>
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}

.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}

.btn {
  text-align: center;
}

.btnStyle {
  margin-top: 10px;
}
</style>
